import { updateUserPassword } from "@/services";
import type { UpdateUserPasswordReq } from "@/services";
import { useUserStore } from "@/store";
import { Button, Form, Input, Toast } from "antd-mobile";
import React from "react";
import { encrypt } from "@/utils";
const UpdatePassword: React.FC = () => {
  const { userInfo } = useUserStore();
  const updatePassWordHandle = async (
    values: Partial<UpdateUserPasswordReq>
  ) => {
    const res = await updateUserPassword({
      onlyId: userInfo.onlyId,
      password: encrypt(values.password!),
      newPassword: encrypt(values.newPassword!),
    });
    if (res.code == 200) {
      Toast.show({
        icon: "success",
        content: "修改成功！",
      });
      history.back();
    }
  };

  return (
    <div>
      <Form
        layout="horizontal"
        onFinish={(values) => {
          updatePassWordHandle(values);
        }}
        footer={
          <Button block type="submit" color="primary" size="large">
            修改密码
          </Button>
        }
      >
        <Form.Item
          name="password"
          label="当前密码"
          rules={[{ required: true, message: "当前密码不能为空" }]}
        >
          <Input placeholder="请输入当前密码" type="password" />
        </Form.Item>
        <Form.Item
          name="newPassword"
          label="新密码"
          rules={[{ required: true, message: "新密码不能为空" }]}
        >
          <Input placeholder="请输入新密码" type="password" />
        </Form.Item>
      </Form>
    </div>
  );
};

export default UpdatePassword;
